<?php

require_once 'System/Forms/StudentFormStep1.php';
require_once 'System/Forms/StudentFormStep2.php';
require_once 'System/Forms/StudentFormStep3.php';

class StudentController extends Controller
{
	protected $requiresAuthentication = true;
	
	/**
	 * 
	 * @see Controller::defaultAction()
	 */
	public function defaultAction()
	{
		$view = new HTMLView( 'StudentController/Overview', $this->getSmarty() );
		$view->assign( 'studenten', Student::find('all') );
		
		$this->getViewManager()->push( $view );
	}
	
	public function linkAction()
	{
		if( !isset( $_GET['id'] ) )
		{
			$this->defaultAction();
			return;
		}
		
		$student = Student::find( $_GET['id'] );
		
		if( $student == NULL )
		{
			$this->defaultAction();
			return;
		}
		
		$form = new Form('post', '');
		$select = new SelectOneChoice( 'oid', 'Selecteer een opdracht', '', '' );
		foreach( Opdracht::find('all') as $opdracht )
		{
			$select->addValue( $opdracht->getSugarId(), $opdracht->name );	
		}
		
		$form->addChild( $select );
		$form->addChild( new SubmitButton('asdf', 'Koppelen') );
		
		if( $_SERVER['REQUEST_METHOD'] == 'POST' )
		{
			$student->opdracht_id = $_POST['oid'];
			$student->save();
			header('Location: ?c=student&id=' . $_GET['id']);
			die();
		}
		
		$view = new HTMLView( 'StudentController/Link', $this->getSmarty() );
		$view->assign( 'student', $student );
		$view->assign( 'form', $form );
		
		$this->getViewManager()->push( $view );
	}
	
	public function createAction()
	{
		$this->showForm( );
	}
	
	public function editAction()
	{
		if( isset( $_GET['id'] ) )
		{
			$student = Student::find( $_GET['id'] );

			if( $student == null )
			{
				throw new Exception('Student with id ' . $_GET['id'] . ' was not found.');
			}
				
			$this->showForm( $student );
		}
		else
		{
			$this->defaultAction();
		}
	}
	
	public function showForm( Student $studentData = null )
	{
		$attributes = array();
		
		if( $studentData != null )
		{
			$attributes = $studentData->getAttributes();
			$view = new HTMLView( 'StudentController/Edit', $this->getSmarty() );
			$view->assign( 'student', $studentData );
		}
		else
		{
			$view = new HTMLView( 'StudentController/Create', $this->getSmarty() );
		}
		
		
		if( !isset( $_SESSION['student_step'] ) || $_SESSION['student_step'] == 1 )
		{
			$this->form_Step1( $view, $attributes );
		}
		elseif( $_SESSION['student_step'] == 2 )
		{
			$this->form_Step2( $view, $attributes );
		}
		elseif( $_SESSION['student_step'] == 3 )
		{
			$this->form_Step3( $view, $attributes );
		}
		else
		{
			$this->form_ViewConfirmation();
		}
	}
	
	private function form_Step1( View $view, $data = array(), $delay = false )
	{
		$student = new StudentFormStep1( $_SERVER['REQUEST_METHOD'] == 'POST' && !$delay ? $_POST : $data );
		
		$view->assignArray( array( 'form' => $student, 'step' => 1 ) );

		$this->getViewManager()->remove( $view );
		$this->getViewManager()->push( $view );
				
		if( !$delay && $_SERVER['REQUEST_METHOD'] == 'POST' && $student->isValid() )
		{
			$student = new Student( $_POST );
			//$student->save();
			$_SESSION['student_step'] = 2;
			$_SESSION['student_temp'] = $student;
			
			// Show the next step.
			$this->form_Step2( $view, $data, true );
			return;
		}
		
		$_SESSION['student_step'] = 1;
	}
	
	private function form_Step2( $view, $data = array(),  $delay = false )
	{
		$form = new StudentFormStep2( $_SERVER['REQUEST_METHOD'] == 'POST' && !$delay ? $_POST : $data );
		
		//$createView = new HTMLView( 'StudentController/Create', $this->getSmarty() );
		$view->assignArray( array( 'form' => $form, 'step' => 2 ) );
		
		$this->getViewManager()->remove( $view );
		$this->getViewManager()->push( $view );
		
		if( !$delay && $_SERVER['REQUEST_METHOD'] == 'POST' )
		{
			if( isset( $_POST['previous'] ) )
			{
				$_SESSION['student_step'] = 1;
				$this->form_Step1( $view, $_SESSION['student_temp']->getAttributes(), true );
				return;
			}
			elseif( $form->isValid() )
			{
				if( !$form->hasSecondaryAddress() && !$delay )
				{
					$_SESSION['student_step'] = 4;
					$_SESSION['student_temp']->update( $_POST );
					$this->showForm( $_SESSION['student_temp'] );
					unset( $_SESSION['student_step'], $_SESSION['student_temp'] );
				}
				else
				{
					$_SESSION['student_step'] = 3;
				}
				
				// Store the fields in the session but don't save yet.
				$_SESSION['student_temp']->update( $_POST, false );
				
				// Show the next step.
				$this->form_Step3( $view, $data, true );
				//echo 'Success';
			}
		}
	}
	
	private function form_Step3(  $view, $data = array(), $delay = false )
	{
		$form = new StudentFormStep3( $_SERVER['REQUEST_METHOD'] == 'POST' ? $_POST : $data );
		
		$finished = false;
		
		if( !$delay && $_SERVER['REQUEST_METHOD'] == 'POST' )
		{
			if( isset( $_POST['previous'] ) )
			{
				$_SESSION['student_step'] = 2;
				$this->form_Step2( $view, $_SESSION['student_temp']->getAttributes(), true );
				return null;
			}
			elseif( $form->isValid() )
			{
				$_SESSION['student_step'] = 'finished';
				$_SESSION['student_temp']->update( $_POST );
				
				$finished = true;
			}
		}
		
		if( !$finished )
		{
			//$view = new HTMLView( 'StudentController/Create', $this->getSmarty() );
			$view->assignArray( array( 'form' => $form, 'step' => 3 ) );
		}
		else
		{
			$this->showForm( $_SESSION['student_temp'] );
			unset( $_SESSION['student_step'], $_SESSION['student_temp'] );
		}
		
		$this->getViewManager()->remove( $view );
		$this->getViewManager()->push( $view );
	}
	
	private function form_ViewConfirmation()
	{
		header('Location: ?c=student&a=detail&id=' . $_SESSION['student_temp']->getSugarId());
	}
	
	public function detailAction()
	{
		if( isset( $_GET['id'] ) )
		{
			$student = Student::find( $_GET['id'] );

			if( $student == null )
			{
				throw new Exception('Student with id ' . $_GET['id'] . ' was not found.');
			}
			$view = new HTMLView( 'StudentController/Detail', $this->getSmarty() );
			$opdracht =  Opdracht::find( $student->opdracht_id );

			if( $opdracht->id )
			{
				$view->assign( 'opdracht', $opdracht );		
			}
			
			$view->assign( 'student', $student );
			$this->getViewManager()->push( $view );
		}
		else
		{
			$this->defaultAction();
		}
	}
	
	public function deleteAction()
	{
		if( isset( $_GET['id'] ) )
		{
			$student = Student::find( $_GET['id'] );

			if( $student == null )
			{
				throw new Exception('Student with id ' . $_GET['id'] . ' was not found.');
			}

			$student->delete();
		}
		
		$this->defaultAction();
	}
}